
/*
利用vue框架制作日历，利用导入js文件的形式未起作用，直接在HTML中嵌入即可；
未知原因，待解决
*/

var calendar = new Vue({
	el: '#calendar',
	data: {
		currentYear: 1970,
		currentMonth: 1,
		currentDay: 1,		//几号
		currentWeek: 1,		//周几,0、1、2...6
		days: []
	},
	created: function(){
		this.initData(null);
	},
	methods: {
		//返回日期，格式为“2016-07-01”
		formatDate: function(year,month,day){
			var y = year,
				m = month,
				d = day;
			if(month < 10) m = '0'+m;
			if(d < 10) d = '0'+d;
			return y+'-'+m+'-'+d;
		},
		//数据初始化
		initData: function(cur){
			var date;
			if(cur){
				date = new Date(cur);
			}else{
				date = new Date();
			}
			this.currentYear = date.getFullYear();
			this.currentMonth = date.getMonth()+1;
			this.currentDay = date.getDate();
			this.currentWeek = date.getDay();
			var str = this.formatDate(this.currentYear,this.currentMonth, this.currentDay);
			console.log('today:'+str+','+this.currentWeek);

			this.days.length = 0;
			for(var i=this.currentWeek; i>=1; i--){
				var d = new Date(str);
				d.setDate(d.getDate()-i);
				console.log('first:'+d.getDate());
				this.days.push(d);
			}
			for(var i=0; i<35-this.currentWeek; i++){
				var d = new Date(str);
				d.setDate(d.getDate()+i);
				this.days.push(d);
			}
		},
		pick: function(date){
			//alert(this.formatDate(date.getFullYear(),date.getMonth()+1,date.getDate()));
			$.ajax({
				type: "POST",
				url: "/kingticket/movie/findByDate",
				data: {date: this.formatDate(date.getFullYear(),date.getMonth()+1,date.getDate())},
				dataType: "json",
				success: function (data) {
					calendarDay.$data.item = data.data;
				},
				error:function(data){
					alert("error");
				}
			});
		},
		//上个月
		pickPre: function(year,month){
			var d = new Date(this.formatDate(year,month,1));
			// setDate(0):上个月最后一天
			// setDate(-1):上个月倒数第二天
			// setDate(d):距离上个与最后一天相隔d天
			d.setDate(0);
			this.initData(this.formatDate(d.getFullYear(),d.getMonth()+1,1));
		},
		//下个月
		pickNext: function(year,month){
			var d = new Date(this.formatDate(year,month,1));
			d.setDate(35);
			this.initData(this.formatDate(d.getFullYear(),d.getMonth()+1,1));
		},
		pickYear: function(year,month){
			alert(year+','+month);
		}
	}
});